package com.teng.mylibrary.ui.adapter

import androidx.recyclerview.widget.RecyclerView
import com.teng.mylibrary.R
import com.teng.mylibrary.base.BaseVBAdapter
import com.teng.mylibrary.base.BaseViewHolder
import com.teng.mylibrary.databinding.ItemChooseMultipleBinding

/**
 * Description:    多项选择adapter
 * Author:         liuteng
 * CreateDate:     2021年9月15日
 * UpdateRemark:   更新说明
 */
class MultipleChooseAdapter(mList: ArrayList<String>) :
    BaseVBAdapter<String>(mList) {

    private val map = hashMapOf<Int, Boolean>()

    override fun getItemView(viewType: Int): Int {
        return R.layout.item_choose_multiple
    }


    fun getSelectedMap(): Map<Int, Boolean> {
        return map
    }


    override fun onBindHolder(holder: BaseViewHolder, position: Int) {
        val binding = holder.dataBinding as ItemChooseMultipleBinding
        binding.cbBox.text = mList[position]
        binding.cbBox.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                map.put(position, true)
            } else {
                map.remove(position)
            }
        }
        binding.cbBox.isChecked = map.containsKey(position)
    }
}